--[[
    上海宝钢 10.46.150.67:8080
]]
require("apm_core")
require("ApmXml")
require("ApmJson")
require("extension")
local httpUtil = require("ApmHttpUtil")

local G_DEBUG = true

if G_DEBUG == false then
    function logDebug(fmt,...) end
    function logInfo(fmt,...) end
    function logWarn(fmt,...) end
    function logError(fmt,...) end
    function logFatal(fmt,...) end
end

local KRED='\x1B[0;31m'
local KSALMON='\x1B[1;31m'
local KGREEN='\x1B[0;32m'
local KLIMEGREEN='\x1B[1;32m'
local KYELLOW='\x1B[0;33m'
local KORANGE='\x1B[0;34m'
local KBLUE='\x1B[1;34m'
local KMAGENTA='\x1B[0;35m'
local KPINK='\x1B[1;35m'
local KCYAN='\x1B[0;36m'
local RESET='\x1B[0m'

local dict_transtype = {}

local function parseIp(ip)
    local _, _, value = string.find(ip, "(%d+.%d+)$")
    return value == nil and "" or value
end

local function detect(s_payload)
    if s_payload == nil then return nil end
    logInfo("######### REQ:["..KYELLOW.."%s"..RESET.."]", s_payload)
    return ApmJson.decode(s_payload)
end

local function parse_req(t_json_tree, s_transtype)
    -- 交易名称
    local s_transtype_name = t_json_tree.attr.efFormCname
    if s_transtype_name == nil then s_transtype_name = s_transtype end
    return s_transtype_name
end

local function parse_resp(t_json_tree)
    -- 交易返回码
    local s_rspcode = t_json_tree.status
    -- 交易返回信息
    local s_rspmsg = t_json_tree.msg
    return s_rspcode, s_rspmsg
end

--- main
function post_analysis()
    local ApmData = require("ApmData")
    if ApmData.getL7Proto() ~= 80 then return end
    if ApmData.getMsgType() ~= 1 then return end

    local s_title = ApmData.getField("transaction.title", 1)
    if not string.endswith(s_title, "/bfpf/EiService") then return end

    local aaaa = ApmData.getField("request.http.body.1", 1)
    logInfo("######### REQ:["..KGREEN.."%s"..RESET.."]", aaaa)

    local t_json_tree
    local s_transtype
    local s_transtype_name
    local s_flowno
    local s_rspcode
    local s_rspmsg

    local t_kv = httpUtil.getParams(ApmData)
    if t_kv ~= nil then
        local s_service = t_kv["service"]
        local s_method = t_kv["method"]
        if s_service ~= nil and s_method ~= nil then
            s_transtype = s_service .. "_" .. s_method

            local s_eiinfo = t_kv["eiinfo"]
            if s_eiinfo ~= nil then
                s_eiinfo = string.trim(s_eiinfo)

                local t_json_tree = detect(s_eiinfo)
                if t_json_tree ~= nil then
                    s_transtype_name = parse_req(t_json_tree, s_transtype)
                end
            end
        end
    else
        logInfo("REQ:["..KRED.."%s"..RESET.."]", aaaa)
    end

    local s_resp = ApmData.getField("response.http.body.1", 1)
    if s_resp ~= nil then
        s_resp = string.trim(s_resp)
        local t_json_tree = detect(s_resp)
        if t_json_tree ~= nil then
            s_rspcode, s_rspmsg = parse_resp(t_json_tree)
        end
    end

    if s_transtype_name == nil then
        local s_req = ApmData.getField("request.http.body.1", 1)
        logInfo("s_transtype_name is nil REQ:["..KRED.."%s"..RESET.."]", s_req)
    elseif s_rspcode == nil then
        logInfo("s_rspcode is nil RESP:["..KBLUE.."%s"..RESET.."]", s_resp)
    else
        ApmData.replaceField("transaction.type_code", s_transtype)
        ApmData.replaceField("transaction.type_name", s_transtype_name)
--        ApmData.replaceField("transaction.title", s_transtype_name)

        ApmData.replaceField("response.code_string", s_rspcode)
        ApmData.delFields("response.code")

        if s_rspmsg ~= nil then
            ApmData.replaceField("response.error_message", s_rspmsg)
        end

        if s_flowno ~= nil then
            ApmData.replaceField("transaction.code", s_flowno)
        end

        logInfo("(%s:%d->%s:%d) name:%s|seq:%s|ret:%s|%s",
            ApmData.getSrcIp(),
            ApmData.getSrcPort(),
            parseIp(ApmData.getDstIp()),
            ApmData.getDstPort(),
            s_transtype_name,
            s_flowno,
            s_rspcode,
            s_rspmsg
        )
    end

    return
end
--150.67:8080
--v1.0.0
